<?php

abstract class PlatformBase
{
    const PLATFORM_SZ_SDK       = 'SzSdk';
    const PLATFORM_H5_WE_CHAT   = 'H5WeChat';
    const PLATFORM_DEV          = 'dev';
    const PLATFORM_FACEBOOK     = 'facebook';
    const PLATFORM_QQ           = 'qq';
    const PLATFORM_TT           = 'tt';
//    const PLATFORM_QQ_SG        = 'QQSG';

    protected $configs;

    protected $appId;

    protected $requestUrl;

    protected $appSecret;
    /**
     * 验证token
     *
     * @param string $platformId 平台ID
     * @param string $token 登录token
     * @return bool
     */
    abstract public function checkToken($platformId, $token);

    /**
     *  平台上传日志
     * @param int $userId
     * @param array (key => $value) $params
     * @return void
     */
    public function sendStatistics($userId, $params)
    {

    }

    /**
     *  获取用户真实货币
     * @param int $userId
     * @param array (key => $value) $params
     * @return int
     */
    public function getMoney($userId)
    {
        /**
         * @var ModulePaymentVo $payment
         */
        $payment = SzPersister::get()->getVo(null, ModulePaymentConst::ORM_NAME_PAYMENT, $userId);
        return !is_null($payment) ? $payment->getMoney() : 0;
    }

    /**
     * Update user money count.
     *
     * @param int $userId
     * @param int $money
     * @param ModulePaymentVo $payment default null
     * @param boolean $logCommodityConsume default true, 如果true，记为价值消耗
     * @param int $commodityValue default 0
     * <pre>
     * 当该参数为0
     *      1. money参数为正数，增加的money必定是免费代币，不需要处理价值流转
     *      2. money参数为负数，扣除的money可能为付费代币，根据付费代币数量的扣除量，扣除价值后，并在返回结果中加入扣除的价值，与扣除的付费代币量
     *      3. 判断$logCommodityConsume，如果为true，则记为价值消耗
     * 当该参数小于0
     *      1. 不可能出现的情况，将该参数调整为0
     * 当该参数大于0
     *      1. money参数为正数，增加的money必定为付费代币，并增加外部传入的价值
     *      2. money参数为负数，不可能出现的情况，如果出现，该参数调整为0
     *      3. 判断$logCommodityConsume，如果为true，则修改为false, 记为价值流入，并在外部进行日志采集
     * </pre>
     * @return array(ModulePaymentVo, int, array($entityId => array($count, $commodityValue), ...))
     */
    public function updateMoney($userId, $money, $payment = null, $logCommodityConsume = true, $commodityValue = 0)
    {
        $money = ceil($money);
        if (is_null($payment)) {
            $payment = SzPersister::get()->getVo(null, ModulePaymentConst::ORM_NAME_PAYMENT, $userId);
        }

        if ($money == 0) {
            return array($payment, 0, array());
        }

        // 判断是否价值流入
        if ($commodityValue > 0 && $money > 0) {
            $logCommodityConsume = false;
        } else {
            $commodityValue = 0; // 不符合要求，重置为0
        }

        // 读取系统配置
        $platform = SzConfig::get()->loadAppConfig('app', 'PLATFORM');

        // 修改前的付费代币数量与价值
        $originPurchasedMoney = $payment->getPurchasedMoney();
        $originCommodityValue = $payment->getCommodityValue();

        ModulePaymentFactory::init($userId, $platform)->updateMoneyAmount($payment, $money, $commodityValue);

        if ($_SERVER['is_payment_callback'] ?? false) $payment->persist();
        SzPersister::get()->setVo($payment);

        // 修改后的付费代币数量与价值
        $nowPurchasedMoney = $payment->getPurchasedMoney();
        $nowCommodityValue = $payment->getCommodityValue();

        $action = ($money > 0) ? GameConst::ENTITY_ACTION_GET : GameConst::ENTITY_ACTION_CONSUME;

        // 付费代币数量变更与价值变更
        $changePurchasedMoney = $originPurchasedMoney - $nowPurchasedMoney;
        $changedCommodityValue = $originCommodityValue - $nowCommodityValue;

        if ($logCommodityConsume && $changedCommodityValue != 0) {
            // 任务进度计数：价值消耗
            SzLogger::get()->debug("-----UpdateMoney-------价值消耗------------");
            StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_FINANCE_CASH, array(
                'clientData' => array(
                    'tid' => GameConst::ENTITY_ACTION_CONSUME_MONEY . ':' . abs($changePurchasedMoney) . '|',
                    'item' => 1,
                    'value' => abs($changedCommodityValue),
                )
            ));
        }

        // 拼接entityId
        $buildEntityId = EntityService::get()->buildEntityId($action, GameConst::ENTITY_TYPE_MONEY,
            GameConst::ENTITY_PADDING_NULL);

        // 付费代币增加，价值为正数
        // 付费代币减少，价值为负数
        return array(
            $payment,
            $changedCommodityValue,
            array(
                $buildEntityId => array(
                    $changedCommodityValue,
                    $changePurchasedMoney
                )
            )
        );
    }
}
